# -*-coding:utf-8-*-
import json
import pandas as pd
import numpy as np
import redis
import sys
import time
from datetime import datetime


pool = redis.ConnectionPool(host='localhost',
                            port=6379,
                            decode_responses=True,
                            db=0)
r = redis.Redis(connection_pool=pool)

symbol = sys.argv[1]
# symbol = 'BTCBUSD'
symbol = symbol.lower()


last_data = []
while True:
    time.sleep(5)
    data = r.lrange(f'{symbol}_trade', 0, 1000)
    new_data = list(set(data) - set(last_data))
    new_data = new_data[::-1]
    if len(new_data) > 0:
        for d in new_data:
            hour = json.loads(data[0])['t'][:13]
            open(f'../trade_data/{symbol}_{hour}_trade.txt', 'a').write(f'{d}\n')
            last_data = data



